//
//  KlineModel.m
//  Futures
//
//  Created by Cheng on 2017/6/23.
//  Copyright © 2017年 Cheng. All rights reserved.
//

#import "KlineModel.h"

@implementation KlineModel

//+ (instancetype)modelWithString:(NSString *)str{
//    NSArray *array = [str componentsSeparatedByString:@","];
//    KlineModel *model = [[self alloc] init];
//    model.dateTime = array[11];
//    model.nowPrice     = [array[1] floatValue];
//    model.preClosePrice = [array[3] floatValue];
//    model.highPrice  = [array[4] floatValue];
//    model.lowPrice   = [array[5] floatValue];
//    model.openPrice     = [array[6] floatValue];
//    model.volume = [array[8] floatValue];
//    return model;
//}

//  calculation by stock object
- (NSArray *)CalculationMALine:(NSArray<KlineModel *> *)models
{
    for (int i = 0; i < models.count; i++) {
        KlineModel *model = models[i];
        model.MA5 = [self averageWithMAData:models MArange:NSMakeRange(i, 5) expectMAProperty:@"Close"];
        model.MA10 = [self averageWithMAData:models MArange:NSMakeRange(i, 10) expectMAProperty:@"Close"];
        model.MA20 = [self averageWithMAData:models MArange:NSMakeRange(i, 20) expectMAProperty:@"Close"];
        model.MAVOL5 = [self averageWithMAData:models MArange:NSMakeRange(i, 5) expectMAProperty:@"Vol"];
        model.MAVOL10 = [self averageWithMAData:models MArange:NSMakeRange(i, 10) expectMAProperty:@"Vol"];
    }
    return  models;
}

- (float)averageWithMAData:(NSArray *)stockArray MArange:(NSRange)MArange expectMAProperty:(NSString *)MAProperty
{
    if (MArange.location < MArange.length - 1) return 0.0f;
    
    float sum = 0.0f;
    
    NSArray *needValueArr = [stockArray subarrayWithRange:NSMakeRange(MArange.location - MArange.length + 1, MArange.length)];
    
    for (KlineModel *model in needValueArr) {
        
        float value = [[model valueForKey:MAProperty] floatValue];
        
        sum += value;
    }
    return sum/MArange.length;
}

//  滑动时重新生成
- (NSMutableArray *)reCalculationMALine:(NSMutableArray *)showArray willShowArray:(NSMutableArray *)willShowArray
{
    NSMutableArray *allArray = [NSMutableArray array];
    // merge first add will
    for (KlineModel *m in willShowArray) {
        [allArray addObject:m];
    }
    for (KlineModel *m in showArray) {
        [allArray addObject:m];
    }
    
    for (int i = 0; i < allArray.count; i++) {
        KlineModel *model =allArray[i];
        model.MA5 = [self averageWithMAData:allArray MArange:NSMakeRange(i, 5) expectMAProperty:@"closePrice"];
        model.MA10 = [self averageWithMAData:allArray MArange:NSMakeRange(i, 10) expectMAProperty:@"closePrice"];
        model.MA20 = [self averageWithMAData:allArray MArange:NSMakeRange(i, 20) expectMAProperty:@"closePrice"];
        model.MAVOL5 = [self averageWithMAData:allArray MArange:NSMakeRange(i, 5) expectMAProperty:@"volume"];
        model.MAVOL10 = [self averageWithMAData:allArray MArange:NSMakeRange(i, 10) expectMAProperty:@"volume"];
    }
    NSArray *array = [allArray subarrayWithRange:NSMakeRange(allArray.count - showArray.count , showArray.count)];
    return [NSMutableArray arrayWithArray:array];
}


@end
